//
//  HelpMeBagApplyFirstStepVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/9.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "HelpMeBagApplyFirstStepVC.h"
#import "HelpMeBagApplySecondStepVC.h"

@interface HelpMeBagApplyFirstStepVC ()
{
    UIButton *button_nextStep;
}

@property (nonatomic,strong) UIView *view_topFirstStepBG;
@property (nonatomic,strong) UITextField *textField_name;
@property (nonatomic,strong) UITextField *textField_idCard;
@end

@implementation HelpMeBagApplyFirstStepVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.title = @"帮我贷";
    
    [self initWithHelpMeBagApplyFirstStepVCSubviews];
    
    //button
    button_nextStep = [[UIButton alloc] initWithFrame:CGRectMake(48*PROPORTION_WIDTH,167*PROPORTION_WIDTH+self.textField_idCard.bottom,kScreenWidth - 48*PROPORTION_WIDTH*2, 93*PROPORTION_WIDTH)];
    [self.view addSubview:button_nextStep];
    
    button_nextStep.backgroundColor = COLOR_UNBUTTON;
    
    [button_nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [button_nextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_nextStep.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    button_nextStep.layer.cornerRadius = 3.0;
    button_nextStep.clipsToBounds = YES;
    
    [button_nextStep addTarget:self action:@selector(ApplayNowNextAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --- 创建视图界面
- (void) initWithHelpMeBagApplyFirstStepVCSubviews
{
    self.view_topFirstStepBG = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 223*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_topFirstStepBG];
    self.view_topFirstStepBG.backgroundColor = [UIColor whiteColor];

    //展位的背景图
    UIImageView *imageView_fullImage = [[UIImageView alloc] initWithFrame:CGRectMake(73*PROPORTION_WIDTH,48*PROPORTION_WIDTH, kScreenWidth-73*PROPORTION_WIDTH*2, 133*PROPORTION_WIDTH)];
    [self.view_topFirstStepBG addSubview:imageView_fullImage];
    imageView_fullImage.image = [UIImage imageNamed:@"step1"];
    
    NSArray *array_title = @[@"姓名 :",@"身份证号 :"];
    for (int i=0; i<2; i++) {
        
        UILabel *label_temp = [[UILabel alloc] initWithFrame:CGRectMake(60*PROPORTION_WIDTH, self.view_topFirstStepBG.bottom+(44+47)*PROPORTION_WIDTH*i+75*PROPORTION_WIDTH,140*PROPORTION_WIDTH,55*PROPORTION_WIDTH)];
        [self.view addSubview:label_temp];
        label_temp.text = array_title[i];
        label_temp.textColor = COLOT_TEXTGRAY;
        label_temp.font = [UIFont systemFontOfSize:Font_SIZE_30];
        label_temp.textAlignment = NSTextAlignmentRight;
    }
    
    //姓名
    self.textField_name = [[UITextField alloc] initWithFrame:CGRectMake(215*PROPORTION_WIDTH, self.view_topFirstStepBG.bottom+75*PROPORTION_WIDTH, 440*PROPORTION_WIDTH, 55*PROPORTION_WIDTH)];
    [self.view addSubview:self.textField_name];

    self.textField_name.backgroundColor = [UIColor whiteColor];
    self.textField_name.layer.cornerRadius = 3.0;
    self.textField_name.clipsToBounds = YES;
    self.textField_name.font = [UIFont systemFontOfSize:Font_SIZE_24];
    
    self.textField_name.tag = 1101;
    [self.textField_name addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    //身份证号
    self.textField_idCard = [[UITextField alloc] initWithFrame:CGRectMake(215*PROPORTION_WIDTH, self.textField_name.bottom+44*PROPORTION_WIDTH, 440*PROPORTION_WIDTH, self.textField_name.height)];
    [self.view addSubview:self.textField_idCard];
    
    self.textField_idCard.backgroundColor = [UIColor whiteColor];
    self.textField_idCard.layer.cornerRadius = 3.0;
    self.textField_idCard.clipsToBounds = YES;
    self.textField_idCard.font = [UIFont systemFontOfSize:Font_SIZE_24];
    self.textField_idCard.tag = 1100;
    [self.textField_idCard addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

}

/**
 *键盘是否亮起
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    switch (textField.tag) {
        case 1100:
            if (textField==self.textField_idCard) {
                if (self.textField_idCard.text.length > 17 ) {
                    textField.text = [textField.text substringToIndex:18];
                    if ( self.textField_idCard.text.length!=0) {
                        button_nextStep.backgroundColor = COLOR_BUTTON;
                        button_nextStep.userInteractionEnabled=YES;
                        
                        [self.mutableDic_pathBagInfo setObject:textField.text forKey:@"idcard"];

                        
                    }
                }
                else
                {
                    button_nextStep.backgroundColor = COLOR_UNBUTTON;
                    button_nextStep.userInteractionEnabled = NO;
                }
            }
            break;
        case 1101:
             [self.mutableDic_pathBagInfo setObject:textField.text forKey:@"name"];
            
            if (textField.text.length>0 &&self.textField_idCard.text.length==18)
            {
                
                button_nextStep.backgroundColor=COLOR_BUTTON;
                button_nextStep.userInteractionEnabled=YES;
                
            }
            else
            {
                button_nextStep.backgroundColor = COLOR_UNBUTTON;
                button_nextStep.userInteractionEnabled=NO;
                
            }
            
            break;
        default:
            break;
    }
}

- (void) ApplayNowNextAction
{
    if ([BaseViewController checkIdentityCardNo:self.textField_idCard.text]) {
    
        HelpMeBagApplySecondStepVC *secondStepView = [[HelpMeBagApplySecondStepVC alloc] init];
    
        secondStepView.mutableDic_pathBagInfo = self.mutableDic_pathBagInfo;
    
        [self.navigationController pushViewController:secondStepView animated:YES];
    }
    else
    {
        [UIView showAlertMsg:@"请输入正确身份证码"];
    }

}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
